""" 图书管理系统主逻辑 """
from user_define import User, AdminUser, NormalUser
from bookshelf_define import BookShelf


def select_login_type() -> str:
    """
    选择登陆类型
    :return: str
    """
    print("+----------------------登录----------------------+")
    print("|欢迎来到图书管理系统，请选择登陆方式：\t\t\t\t|")
    print("|1：普通用户登录\t\t2：管理员登录\t\t3：账号注册\t|")
    print("+-----------------------------------------------+")
    print("==> : 请输入您的选择：", end='')
    choose = input()
    return choose


def read_file(path: str) -> dict:
    """
    读取账户信息文件
    :return: 账户信息字典
    """
    with open(f"./{path}", "r", encoding="UTF-8") as f:
        # 读取文件中的账号数据
        password_dict = dict()
        for line in f.readlines():
            line.strip()    # 去除换行符
            tmp_list = line.split(',')
            password_dict[tmp_list[0]] = tmp_list[1]

        return password_dict


def login():
    while True:
        # 调用select_login_type,选择登陆方式
        choose = select_login_type()

        if choose == '1':

            # 获取账户文件
            password_dict = read_file('normal_pw.txt')

            # 输入账号、密码，尝试登录
            count = 3   # 输入次数
            while count != 0:
                account_name = input("请输入用户名：")
                password = input("请输入密码：")
                if account_name in password_dict.keys():
                    if password_dict[account_name] == password:
                        print("登录成功！")
                        return NormalUser(account_name, password)
                    else:
                        count -= 1
                        print(f"密码错误，请重新输入！你还有{count}次机会。")
                else:
                    count -= 1
                    print(f"账号{account_name}不存在，请验证后重试！你还有{count}次机会。")

        elif choose == '2':

            # 获取账户文件
            password_dict = read_file('admin_pw.txt')
            # 输入账号、密码，尝试登录
            count = 3   # 输入次数
            while count != 0:
                account_name = input("请输入用户名：")
                password = input("请输入密码：")
                if account_name == 'root':     # 账户名为root
                    if password_dict[account_name] == password:
                        print("登录成功！")
                        return AdminUser(account_name, password)
                    else:
                        count -= 1
                        print(f"密码错误，请重新输入！你还有{count}次机会。")
                else:
                    count -= 1
                    print(f"不存在管理员账号{account_name}，请验证后重试！你还有{count}次机会。")

        elif choose == '3':
            # 注册账户
            print("请输入您需要注册的账户名：", end='')
            new_name = input()
            print("请输入您的密码：", end='')
            new_password = input()
            with open("./normal_pw.txt", "a", encoding="UTF-8") as f:
                f.write("\n")
                f.write(f"{new_name},{new_password}")
            print("注册成功，重新登录即可！")

        else:
            print("您的输入有误，请重新输入！")


def main():
    """
    以title为唯一主键
    books表中 0为在库，1为借阅
    :return:
    """
    # 登录
    user = login()

    # 执行操作
    shelf = BookShelf()     # 书架类，仅可实例化一次！
    shelf.init_shelf()      # 对书架进行初始化插入（仅一次）
    while True:
        choose = user.menu()
        user.doOperation(choose, shelf)
        if choose == '0':
            return None


if __name__ == '__main__':
    main()
